/*************************************************************************
    > File Name: daytimetcpcli.c
    > Author: Xing.Dai
    > Mail: Xing.Dai@aliyun.com 
    > Created Time: 2017-03-27 23:43
 ************************************************************************/

#include "unp.h"

int main(int argc, char **argv)
{
	int sockfd, n;
	char rcvline[MAXLINE + 1];
	struct sockaddr_in srvaddr;

	if(argc != 2){
		err_quit("usage: a.out <IP address>");
	}
	
	if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
		err_sys("socket error.");
	}

	bzero(&srvaddr, sizeof(srvaddr));
	srvaddr.sin_family = AF_INET;
	srvaddr.sin_port = htons(13);

	if(inet_pton(AF_INET, argv[1], &srvaddr.sin_addr) <= 0){
		err_quit("inet_pton err for %s", argv[1]);
	}
	
	if(connect(sockfd, (SA *)&srvaddr, sizeof(srvaddr))){
		err_sys("connect error");
	}

	while( (n = read(sockfd, rcvline, MAXLINE)) > 0){
		rcvline[n] = 0;
		if(fputs(rcvline, stdout) == EOF){

			err_sys("fputs error");
		}
	}

	if(n < 0){

		err_sys("read error");
	}

	exit(0);
}
